package br.com.smanager.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.smanager.dao.common.DataAccessObject;
import br.com.smanager.vo.TimeVO;
import br.com.smanager.vo.common.ValueObject;

public class TimeDAO extends DataAccessObject{
	private TimeVO timeVO = null;
	private boolean result = false;
	
	@Override
	public boolean insert(ValueObject valueObject) {
		timeVO = (TimeVO) valueObject;
		result = true;

		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			session.beginTransaction();
			
			Query qry = session.createSQLQuery("INSERT INTO time(id_time, nome)"+
							                   "VALUES (nextval('sq_time'),?)");
			qry.setString(0, timeVO.getNome());
			
			qry.executeUpdate();

			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			result = false;
		}

		timeVO = null;
		return result;
	}

	@Override
	public boolean update(ValueObject valueObject) {
		timeVO = (TimeVO) valueObject;
		result = true;

		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			session.beginTransaction();

			Query qry = session.createSQLQuery("");
			qry.executeUpdate();

			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			result = false;
		}

		timeVO = null;
		return result;
	}

	@Override
	public boolean delete(ValueObject valueObject) {
		timeVO = (TimeVO) valueObject;
		result = true;

		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			session.beginTransaction();

			Query qry = session.createSQLQuery("");
			qry.executeUpdate();

			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			result = false;
		}

		timeVO = null;
		return result;
	}

	@Override
	public boolean delete(int id) {
		result = true;

		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			session.beginTransaction();

			Query qry = session.createSQLQuery("");
			qry.executeUpdate();

			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			result = false;
		}

		return result;
	}

	@Override
	public ValueObject returnById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ValueObject> returnAll() {
		// TODO Auto-generated method stub
		return null;
	}
}
